package redisinit

import (
	"context"
	"fmt"
	"strconv"
	"tiktok_srv/tiktok"
)

func CartAdd(Name, Status, Cover string, Price float64, Stock, UserId, TikTokId int64) error {
	key := fmt.Sprintf("cart:user_id_%d:tiktok_id_%d", UserId, TikTokId)
	return Rdb.HMSet(context.Background(), key, map[string]interface{}{
		"Name":    Name,
		"Status":  Status,
		"Cover":   Cover,
		"Price":   Price,
		"Stock":   Stock,
		"UserId":  UserId,
		"TiTokId": TikTokId,
	}).Err()
}
func List(UserId int64) (cartlist []*tiktok.CartList, err error) {
	key := fmt.Sprintf("cart:user_id_%d:tiktok_id_*", UserId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		lists := Rdb.HGetAll(context.Background(), s).Val()
		Name := lists["Name"]
		Status := lists["Status"]
		Cover := lists["Cover"]
		Price, _ := strconv.ParseFloat(lists["Price"], 64)
		Stock, _ := strconv.Atoi(lists["Stock"])
		UserId, _ := strconv.Atoi(lists["UserId"])
		TikTokId, _ := strconv.Atoi(lists["TikTokId"])
		cartlist = append(cartlist, &tiktok.CartList{
			Name:     Name,
			Status:   Status,
			Stock:    int64(Stock),
			Price:    float32(Price),
			Cover:    Cover,
			UserId:   int64(UserId),
			TikTokId: int64(TikTokId),
		})
	}
	return
}
